package cz.muni.fi.pa165.ddtroops.daoclasses;

import cz.muni.fi.pa165.ddtroops.daointerfaces.SkillDAO;
import cz.muni.fi.pa165.ddtroops.entities.Skill;
import java.util.List;
import org.springframework.stereotype.Repository;

/**
 * Class SkillDAOImpl serves as a Data Access Object class for entity Skill.
 * SkillDAOImpl class extends generic abstract class BaseDAOImpl. See SkillDAO
 * for set of supported methods.
 *
 * @author Jaromír Svoboda
 */
@Repository(value = "skillDAO")
public class SkillDAOImpl extends BaseDAOImpl<Skill> implements SkillDAO
{
    @Override
    protected boolean isValid(Skill s){
        return s.getDescription() != null &&
               s.getMinXP() != null &&
               s.getName() != null &&
               s.getProfession() != null &&
               !s.getDescription().isEmpty() &&
               !s.getName().isEmpty() &&
               !s.getProfession().isEmpty();
    }

    @Override
    protected boolean isIn(Skill object) {
        return getById(object.getId()) != null;
    }

    public List<Skill> getByProfession(String profession) {
        return this.getEntityManager().createQuery("SELECT c FROM Skill c WHERE c.profession=:profession", Skill.class)
                .setParameter("profession", profession)
                .getResultList();
    }

    public List<Skill> getByXp(Long xp) {
        return this.getEntityManager().createQuery("SELECT c FROM Skill c WHERE c.minXP<=:xp", Skill.class)
                .setParameter("xp", xp)
                .getResultList();
    }

    public List<Skill> getByProfessionAndXp(String profession, Long xp) {
        return this.getEntityManager().createQuery("SELECT c FROM Skill c WHERE c.profession=:profession AND c.minXP<=:xp", Skill.class)
                .setParameter("profession", profession).setParameter("xp", xp)
                .getResultList();
    }
}
